home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch07
/
fig07_06.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
767b
|
29 lines
1 // Fig. 7.6: fig07_06.cpp
2 // Non-friend/non-member functions cannot access
3 // private data of a class.
4 #include <iostream.h>
5
6 // Modified Count class
7 class Count {
8 public:
9 Count() { x = 0; } // constructor
10 void print() const { cout << x << endl; } // output
11 private:
12 int x; // data member
13 };
14
15 // Function tries to modify private data of Count,
16 // but cannot because it is not a friend of Count.
17 void cannotSetX( Count &c, int val )
18 {
19 c.x = val; // ERROR: 'Count::x' is not accessible
20 }
21
22 int main()
23 {
24 Count counter;
25
26 cannotSetX( counter, 3 ); // cannotSetX is not a friend
27 return 0;
28 }